package com.accompany.validator;

import cn.hutool.core.util.StrUtil;
import com.accompany.common.enums.ErrorConstants;
import com.accompany.domain.pojo.TbCourse;
import com.accompany.validator.annotation.ValidateCourseRequest;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class CourseRequestValidator implements ConstraintValidator<ValidateCourseRequest, TbCourse> {

    @Override
    public boolean isValid(TbCourse tbCourse, ConstraintValidatorContext constraintValidatorContext) {
        log.info("参数校验");
        if (StrUtil.isBlank(tbCourse.getCode())) {
            setConstraintViolation(constraintValidatorContext, ErrorConstants.COURSE_CODE_IS_NULL);
            return false;
        }
        return true;
    }

    private void setConstraintViolation(ConstraintValidatorContext context, ErrorConstants errorConstants) {
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(errorConstants.getMessage()).addConstraintViolation();
    }
}
